﻿using System;
namespace WhiteFox.DAlessio.Patterns.UI.MVC
{
    public interface IController { }
    public abstract class Controller<T> : IController
    {
        private Domain<T> domain;
        
        public delegate void ModelChangeDelegate(object sender, ModelChangeEventArgs e);
        public event ModelChangeDelegate ModelChangeEvent;

        #region Singleton
        public Domain<T> Domain
        {
            get
            {
                if (domain == null)
                    domain = new Domain<T>();

                return domain;
            }
        }        
        #endregion

        public void RegisterView(IView view)
        {
            this.ModelChangeEvent += new ModelChangeDelegate(view.ModelChange);
        }
        public void UnregisterView(IView view)
        {
            this.ModelChangeEvent -= new ModelChangeDelegate(view.ModelChange);
        }
        public void RaiseModelChange(object sender, ModelChangeEventArgs e)
        {
            if (ModelChangeEvent != null)
                ModelChangeEvent(sender, e);
        }

    }

    public class ModelChangeEventArgs : EventArgs
    {
        private bool _forceChange;

        public bool ForceChange
        {
            get { return _forceChange; }
        }

        public ModelChangeEventArgs(bool forceChange)
        {
            _forceChange = forceChange;
        }

        public ModelChangeEventArgs()
        {
            _forceChange = false;
        }
    }
}
